package development;

import state.Clip;
import state.GameForm;
import state.Graphics;
import state.Key;
import state.component.GameScrollBar;
import state.font.FontManager;
import state.font.ImageText;
import state.util.GameColor;
import state.util.Hashtable;
import development.socket.SocketClientUtil;

public class FrmGuide extends GameForm {
  private final int STEP = 2;
  private final int DEFAULT_TEXT_X = 10;
  private final int DEFAULT_TEXT_Y = 35;

  public String helpText = "";
  public String title;

  private int textY;

  private ImageText text8;
  private GameScrollBar scrollBar;
  private Clip textView; // Là khung nhìn để vẽ đoạn text ra
  public static GameForm backForm;
  
  private static GameForm singleton;

  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmGuide();
    }
    return singleton;
  }

  public void init(Hashtable parameters) {
    helpText = "";
    String helpId = String.valueOf(parameters.get("helpId"));
    SocketClientUtil.requestHelpContent(helpId);
    title = (String) parameters.get("title");
    if (title == null) {
      title = "";
      if (Global.currentGame != null) {
        title = "Hướng dẫn game " + Global.currentGame.getName();
      }
    }

    textView = new Clip(DEFAULT_TEXT_X, DEFAULT_TEXT_Y, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT - DEFAULT_TEXT_Y - 30); // Là khung nhìn để vẽ đoạn text ra
    scrollBar = new GameScrollBar(Constants.SCREEN_WIDTH - 5, 25, 5, Constants.SCREEN_HEIGHT - 50, textView, _manager);

    textY = DEFAULT_TEXT_Y;
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    DrawUtils.setBottomBar(_manager, null, null, "Trở về");
    _manager.enableTouchPad();
  }

  public void draw(Graphics g) {
    DrawUtils.drawBackground(g);
    DrawUtils.drawTopBar(g);

    text8.setBold(true);
    text8.drawString2(g, title, GameColor.WHITE_CODE, Constants.SCREEN_WIDTH / 2, 3, Constants.TOP_HCENTER_ANCHOR);

    text8.setBold(false);
    text8.drawParagraph2(g, helpText, GameColor.WHITE_CODE, Constants.SCREEN_WIDTH - DEFAULT_TEXT_X, DEFAULT_TEXT_X, textY, scrollBar);
    scrollBar.draw(g);
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.SOFT_RIGHT:
      Global.goBack(backForm);
      backForm = null;
      break;
    case Key.DOWN:
      if (scrollBar.canScrollUp()) {
        textY -= STEP;
      } else {
        textY = scrollBar.getMinTextY();
      }
      break;
    case Key.UP:
      if (scrollBar.canScrollDown()) {
        textY += STEP;
      } else {
        textY = textView._y;
      }
      break;
    }
  }

  public void keyRepeated(int keyCode) {
    switch (keyCode) {
    case Key.DOWN:
      if (scrollBar.canScrollUp()) {
        textY -= 2 * STEP;
      } else {
        textY = scrollBar.getMinTextY();
      }
      break;
    case Key.UP:
      if (scrollBar.canScrollDown()) {
        textY += 2 * STEP;
      } else {
        textY = textView._y;
      }
      break;
    }
  }

  public void destroy() {
    helpText = "";
    title = null;
    text8 = null;
  }
}
